/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.user.api.controller;

import com.alipay.api.AlipayApiException;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.yami.shop.bean.bo.PayInfoBo;
import com.yami.shop.common.enums.PayType;
import com.yami.shop.service.PayManagerService;
import com.yami.shop.user.comment.service.UserLevelService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;


@ApiIgnore
@RestController
@RequestMapping("/notice/levelPay")
@AllArgsConstructor
public class LevelPayNoticeController {

    private final UserLevelService userLevelService;
    private final PayManagerService payManagerService;

    /**
     *
     * 支付成功回调更新会员等级信息
     * @param xmlData
     * @return
     * @throws WxPayException
     */
    @RequestMapping("/levelPayCallback/{payType}")
    public ResponseEntity<String> orderCard(HttpServletRequest request, @PathVariable Integer payType, @RequestBody String xmlData) throws WxPayException, UnsupportedEncodingException, AlipayApiException {

        PayInfoBo payInfoBo = payManagerService.validateAndGetPayInfo(request,PayType.instance(payType),xmlData);

        userLevelService.paySuccess(payInfoBo.getPayNo());
        return ResponseEntity.ok(payInfoBo.getSuccessString());
    }
}
